Random Numbers Within a Specific Range

您所在的位置:网站首页 range matlab Random Numbers Within a Specific Range

Random Numbers Within a Specific Range

#Random Numbers Within a Specific Range | 来源: 网络整理| 查看: 265

By default, rand returns normalized values (between 0 and 1) that are drawn from a uniform distribution. To change the range of the distribution to a new range, (a, b), multiply each value by the width of the new range, (b – a) and then shift every value by a.

First, initialize the random number generator to make the results in this example repeatable.

rng(0,'twister');

Create a vector of 1000 random values. Use the rand function to draw the values from a uniform distribution in the open interval, (50,100).

a = 50; b = 100; r = (b-a).*rand(1000,1) + a;

Verify the values in r are within the specified range.

r_range = [min(r) max(r)]r_range = 50.0261 99.9746

The result is in the open interval, (50,100).

Note

Some combinations of a and b make it theoretically possible for your results to include a or b. In practice, this is extremely unlikely to happen.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3